package com.zxjbyte.yiyi.framework.banner.core;

import cn.hutool.core.net.NetUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author zhangxingjia
 * @Date 2023/2/27 16:17
 * @Version: 1.0
 */
@Slf4j
public class BannerApplicationRunner implements ApplicationRunner{

    @Override
    public void run(ApplicationArguments args) throws Exception{
        long endTime = System.currentTimeMillis();
        ThreadUtil.execute(() -> {
            ThreadUtil.sleep(1, TimeUnit.SECONDS);
            long time = 0L;
            try {
                Class<?> clazz = Class.forName("com.zxjbyte.yiyi.server.YiyiServerApplication");
                long startTime = (long) ReflectUtil.getStaticFieldValue(ReflectUtil.getField(clazz, "startTime"));
                time = endTime - startTime;
            } catch (Throwable ignored) {
            }
            String ip = NetUtil.getLocalhostStr();
            String port = SpringUtil.getProperty("server.port");
            String path = StrUtil.nullToEmpty(SpringUtil.getProperty("server.servlet.context-path"));
            path = "/".equals(path) ? "" : path;
            List<Object> list = new ArrayList<>();
            String cTime = "";
            if(time > 0L){
                cTime = ", time consuming [{} ms]";
                list.add(time);
            }
            list.add("http://"+ ip + ":" + port + path + "/doc.html");
            list.add("http://"+ ip + ":" + port + path + "/actuator");
            log.info("\n------------------------------------------------------------\n" +
                            "Project started successfully" + cTime + "\n" +
                            "Interface doc:  {} \n" +
                            "Actuator path:  {} \n" +
                            "------------------------------------------------------------", list.toArray());
        });
    }



}
